﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Reflection;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace FileMonitor.Configuration.Prototype
{

     //<add name="IPPDMRP007" DataFilter="" Filter="*.txt" SplitArr="" BizProcesses="" TargetDatabaseName="DefaultExchangeDB" 
    //SplitChar="^" TargetTable="SAPForecastT" PlantCodeIndex="0"
     //      BackUpPath="D:\SICT\Files\IPPDL10MRP007\backup" MonitorPath="D:\SICT\Files\IPPDL10MRP007"
     //      typeName="TXTMonitor" WaitTime="5" PlantCodeLength="4" LogPath="D:\SICT\Files\IPPDL10MRP007\Log" DataType="IPPD015FKS" />

    public enum SplitFlag
    {
        CHAR,
        LENGTH
    }

    public class FileProcessConfiguration
    {
        private static Dictionary<string, PropertyInfo> propertyInfoCache=new Dictionary<string, PropertyInfo>();
        private static object lockObj = new object();
        private SplitFlag _splitFlag;
        private string _splitArr;
        public string Name { get;  set; }
        private string _targetDatabaseName;
        private SqlDatabase _targetDatabase;
        private string _fileType;
        private string _filter;

        public string DataType { get;  set; }

        public string Filter
        {
            get
            {
                return this._filter;
            }
            set
            {
                this._filter = value;
                if (string.IsNullOrEmpty(this._fileType))
                {
                    switch (value.ToUpper())
                    {
                        case "*.TXT":
                            this._fileType = "TXT";
                            break;
                        case "*.XLS":
                            this._fileType = "XLS";
                            break;
                        case "*.XLSX":
                            this._fileType = "XLS";
                            break;
                        default:
                            this._fileType = "TXT";
                            break;
                    }
                }
            }
        }
        public string DataFilter { get;  set; }

        public string SplitArr
        {
            get
            {
                return this._splitArr;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    _splitFlag = SplitFlag.LENGTH;
                    this._splitArr = value;
                }
            }
        }

        public FileProcessConfiguration()
        {
            this._splitFlag = SplitFlag.CHAR;
        }

        public string BizProcesses { get;  set; }

        public string TargetDatabaseName 
        {
            get
            {
                return this._targetDatabaseName;
            }
            set
            {
                try
                {
                    this._targetDatabase = FileMonitor.Configuration.Providers.ProviderService.GetProvider().GetAllDataBase()[value];
                    this._targetDatabaseName = value;
                }
                catch (Exception ex)
                {
                    throw new Exception("Can't find the database:"+value);
                }
            }
        }

        public SqlDatabase TargetDatabase { get { return this._targetDatabase; } }

        public string SplitChar { get;  set; }

        public string TargetTable { get;  set; }

        public string PlantCodeIndex { get;  set; }

        public string BackUpPath { get;  set; }

        public string MonitorPath { get;  set; }

        public string LogPath { get; set; }

        public string typeName { get;  set; }

        public string WaitTime { get;  set; }

        public string PlantCodeLength { get; set; }

        public string FilterPlantCode { get; set; }

        public string FileType 
        {
            get
            { 
                return this._fileType; 
            }
            set 
            {
                this._fileType = value;
            }
        }

        public SplitFlag SplitFlag
        {
            get { return this._splitFlag; }
        }

        public string ProcessDataProvider
        {
            get { return "Default"; }
        }


        public void Initialize(NameValueCollection allConfigs)
        {
            foreach (string name in allConfigs.AllKeys)
            {
                PropertyInfo pi = GetPropertyInfo(name);
                if (pi != null)
                {
                    pi.SetValue(this, allConfigs[name], null);
                }

            }
        }

        private PropertyInfo GetPropertyInfo(string name)
        {
            try
            {
                if (!FileProcessConfiguration.propertyInfoCache.ContainsKey(name))
                {
                    lock (lockObj)
                    {
                        if (!FileProcessConfiguration.propertyInfoCache.ContainsKey(name))
                        {
                            PropertyInfo pi = this.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
                            FileProcessConfiguration.propertyInfoCache.Add(name, pi);
                        }
                    }
                }
                return FileProcessConfiguration.propertyInfoCache[name];
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}
